﻿using BasicData.ErrorCode;
using BasicData.Write.API.Appplications.Commands;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NLog.LayoutRenderers;
using System.Security.Claims;

namespace BasicData.Write.API.Controllers
{
    /// <summary>
    /// 控制器-写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MangeController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<MangeController> logger;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public MangeController(IMediator mediator, ILogger<MangeController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 测试
        /// </summary>
        /// <returns>返回结果</returns>
        [HttpGet]
        public IActionResult GetTest()
        {
            try
            {
                logger.LogInformation("调用了一下写的方法");
                int num = int.Parse("abc");
            }
            catch (Exception ex)
            {
                logger.LogError("调用测试方法报错" + ex.Message);

            }
            return Ok("测试");
        }
        /// <summary>
        /// 创建命令
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回添加应用</returns>
        [HttpPost]
        public async Task<APIResult<int>> Addapp(AppCreateCommand command)
        {
            //string UserId = User.FindFirstValue("UserId");
            //string UserNickname = User.FindFirstValue("UserNickname");
            //command.CurrentUser.Add(UserId, UserNickname);


            //从JWT取值
            string UserId = User.FindFirstValue("UserId");
            string UserNickname = User.FindFirstValue("UserNickname");
            string RoleId = User.FindFirstValue("RoleId");
            command.UserId =UserId;
            command.UserNickname = UserNickname;
        
            return await  mediator.Send(command);
        }
    }
}
